<template>
  <div>
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> bab7c7a15a7723f417b1c3b2179ae51ae1f154c9
    <!-- close :所有的、任意形式的-->
    <el-dialog
      :title="isAdd ? '添加角色' : '修改角色'"
      :visible.sync="info.isShow"
      width="30%"
      @close="cancel"
    >
<<<<<<< HEAD
=======
=======

    <!-- close :所有的、任意形式的-->
    <el-dialog :title="isAdd?'添加管理员':'修改管理员'" :visible.sync="dialogVisible" width="30%" @close="cancel">
>>>>>>> d519b3a9147c56fc3b4298a9549da21438cdf484
>>>>>>> bab7c7a15a7723f417b1c3b2179ae51ae1f154c9
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="rolename">
          <el-input v-model="ruleForm.rolename"></el-input>
        </el-form-item>
        <el-form-item label="权限" prop="menus">
          <!--data 数据  -->
          <!-- show-checkbox 显示选框  -->
          <!--  default-expand-all 默认展开所有 -->
          <!--   highlight-current 突出显示当前选项 -->
          <!--   props 配置项 -->
          <el-tree
            :data="roleData"
            show-checkbox
            default-expand-all
            node-key="id"
            ref="tree"
            highlight-current
            :props="defaultProps"
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入负责添加角色的函数
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> bab7c7a15a7723f417b1c3b2179ae51ae1f154c9
import { roleAdd, roleEdit } from "../../request";
let defaultData = {
  rolename: "",
  menus: [],
  status: 1,
};
export default {
  props: {
    info: {
      type: Object,
      default: {
        isAdd: false,
        isShow: false,
      },
    },
  },
<<<<<<< HEAD
=======
=======
import { roleAdd } from "../../request";
let defaultData={
  rolename:'',
  menus:[],
  status:1,
}
export default {
>>>>>>> d519b3a9147c56fc3b4298a9549da21438cdf484
>>>>>>> bab7c7a15a7723f417b1c3b2179ae51ae1f154c9
  data() {
    return {
      dialogVisible: false,
      // 表单的数据
      ruleForm: {
<<<<<<< HEAD
        ...this.defaultData,
=======
<<<<<<< HEAD
        ...this.defaultData,
=======
        rolename: "",
        status: 1,
        menus: [],
>>>>>>> d519b3a9147c56fc3b4298a9549da21438cdf484
>>>>>>> bab7c7a15a7723f417b1c3b2179ae51ae1f154c9
      },
      // 表单的规则
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
      },

      // 树形控件的角色权限
      roleData: [],
      defaultProps: {
        // 子节点
        children: "children",
        // 文字内容
        label: "title",
      },
<<<<<<< HEAD
      // 弹窗类型，添加还是修改
      isAdd: true,
=======
<<<<<<< HEAD
      // 弹窗类型，添加还是修改
      isAdd: true,
=======
  // 弹窗类型，添加还是修改
      isAdd:true
>>>>>>> d519b3a9147c56fc3b4298a9549da21438cdf484
>>>>>>> bab7c7a15a7723f417b1c3b2179ae51ae1f154c9
    };
  },

  created() {
    // 获取下拉列表的数据
    this.getMenuList();
  },
  // mounted() {
  //   // 设置树形控件中被选中的节点
  //   this.setKeys();
  // },
  methods: {
    getMenuList() {
      //gat请求带参数的方式
      //  另一种方式get("/menulist?istree=1")
      this.$http.get("/menulist?istree=1").then((res) => {
        // 获取完整的权限列表，用于分配权限
        this.roleData = res.list;
      });
    },

    // 设置当前组件状态
    setStatus(val) {
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> bab7c7a15a7723f417b1c3b2179ae51ae1f154c9
      // this.dialogVisible = val.isShow;
      // this.isAdd = val.isAdd

      this.ruleForm = { ...val };
      // vue中dom更新时异步的过程，nextTick就是等dom更新完成之后进行的操作
      this.$nextTick(() => {
        // split方法，将字符串转成数组
        this.$refs.tree.setCheckedKeys(this.ruleForm.menus.split(","));
      });
<<<<<<< HEAD
=======
=======
      this.dialogVisible = val.isShow;
      this.isAdd = val.isAdd
>>>>>>> d519b3a9147c56fc3b4298a9549da21438cdf484
>>>>>>> bab7c7a15a7723f417b1c3b2179ae51ae1f154c9
    },
    // 提交表单
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // 获取被勾选的节点，格式是数组
          // getCheckedKeys是element自己的方法
          this.ruleForm.menus = this.$refs.tree.getCheckedKeys();
          // 封装请求
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> bab7c7a15a7723f417b1c3b2179ae51ae1f154c9
          let res;
          if (this.info.isAdd) {
            res = roleAdd(this.ruleForm);
          } else {
            res = roleEdit(this.ruleForm);
          }
          if (res) {
            // 重置弹窗数据
            this.cancel();
            // 关闭弹窗
            this.info.isShow = false;
<<<<<<< HEAD
=======
=======
          let res = roleAdd(this.ruleForm);
          if (res) {
            // 重置弹窗数据
            this.cancel();
>>>>>>> d519b3a9147c56fc3b4298a9549da21438cdf484
>>>>>>> bab7c7a15a7723f417b1c3b2179ae51ae1f154c9
            // 兄弟组件传值 告诉list组件更新数据
            this.$bus.$emit("update");
          }
        } else {
          return false;
        }
      });
    },
    cancel() {
      this.ruleForm = { ...defaultData };
      this.$refs.tree.setCheckedKeys([]);
      // 关闭弹窗
      this.dialogVisible = false;
<<<<<<< HEAD
          // 关闭弹窗
      this.info.isShow=false
=======
>>>>>>> bab7c7a15a7723f417b1c3b2179ae51ae1f154c9
    },
    // 设置选中的
    setKeys() {
      this.$refs.tree.setCheckedKeys(this.ruleForm.menus);
    },
  },
};
</script>

<style>
</style>